home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / snoopdos_source / testcalls.c < prev   
C/C++ Source or Header  |  1996-02-16  |  11KB  |  478 lines

  1. /*
  2.  *        TESTCALLS.C                                                vi:ts=4
  3.  *
  4.  *      Copyright (c) Eddy Carroll, September 1994.
  5.  *
  6.  *        This file simply performs a variety of calls to every function
  7.  *        which can be patched by SnoopDos. It can be asked to do specific
  8.  *        calls, or it can march through the whole lot.
  9.  *
  10.  *        It's used to ensure that our patch code executes correctly.
  11.  *
  12.  *        Usage: TestCalls [opts] [funcs to test]
  13.  */
  14.  
  15. #include "system.h"
  16.  
  17. char Version[] = "$VER: TestCalls 1.1 (19.3.94)";
  18.  
  19. char HelpMsg[] = 
  20. "TestCalls 1.0 by Eddy Carroll. Performs function calls to test SnoopDos.\n"
  21. "\n"
  22. "Usage: TestCalls [-fail | -succeed] [#] [Dos | System | All | <names> ]\n"
  23. "\n"
  24. "-fail    means only call functions with parameters known to return failure.\n"
  25. "-succeed means only call functions with parameters known to return success.\n"
  26. "\n"
  27. "If a number (#) is specified, the tests are repeated that number of times.\n"
  28. "Dos, System and All select the function groups to be tested. Instead of\n"
  29. "selecting one of these groups, you can also list one or more function\n"
  30. "names from the following list:\n"
  31. "\n"
  32. "System functions:\n"
  33. "\n"
  34. "  FindPort      FindSemapore  LockScreen    OpenFont       OpenResource\n"
  35. "  FindResident  FindTask      OpenDevice    OpenLibrary    ReadToolTypes\n"
  36. "\n"
  37. "DOS functions:\n"
  38. "\n"
  39. "  CurrentDir    GetVar        MakeDir       Rename         System\n"
  40. "  DeleteFile    LoadSeg       MakeLink      RunCommand\n"
  41. "  Execute       Lock          Open          SetVar\n"
  42. "\n";
  43.  
  44. #define END_FUNC    0
  45. #define SYS_FUNC    (1 << 0)
  46. #define DOS_FUNC    (2 << 0)
  47. #define ALL_FUNC    (SYS_FUNC | DOS_FUNC)
  48.  
  49. typedef void (*TestFunc)(int, int);
  50.  
  51. /*
  52.  *        Now the test functions
  53.  */
  54. #define TF(name)    void Test_##name(int succ, int fail) {}
  55.  
  56. TF(CurrentDir)
  57. TF(DeleteFile)
  58. TF(Execute)
  59. TF(GetVar)
  60. TF(LoadSeg)
  61. TF(Lock)
  62. TF(MakeDir)
  63. TF(MakeLink)
  64. TF(Rename)
  65. TF(RunCommand)
  66. TF(SetVar)
  67. TF(System)
  68.  
  69. /*
  70.  *        Open
  71.  */
  72. void Test_Open(int trysucc, int tryfail)
  73. {
  74.     BPTR file;
  75.     char uniquename[100];
  76.  
  77.     sprintf(uniquename, "Ram:Testfile-%08x", FindTask(0));
  78.  
  79.     if (trysucc) {
  80.         file = Open(uniquename, MODE_NEWFILE);
  81.         if (file)
  82.             Close(file);
  83.         else
  84.             printf("Warning: Open(\"%s\", write) failed.\n", uniquename);
  85.         file = Open(uniquename, MODE_OLDFILE);
  86.         if (file)
  87.             Close(file);
  88.         else
  89.             printf("Warning: Open(\"%s\", read) failed.\n", uniquename);
  90.         file = Open(uniquename, MODE_READWRITE);
  91.         if (file)
  92.             Close(file);
  93.         else
  94.             printf("Warning: Open(\"%s\", modify) failed.\n", uniquename);
  95.     }
  96.     if (tryfail) {
  97.         file = Open("ram:X/Y/Z/TestCalls-test", MODE_NEWFILE);
  98.         if (file) {
  99.             printf("Warning: Open(\"Ram:X/Y/Z/TestCalls-test\", write) "
  100.                    "succeeded.\n");
  101.             Close(file);
  102.         }
  103.         file = Open("ram:X/Y/Z/TestCalls-test", MODE_OLDFILE);
  104.         if (file) {
  105.             printf("Warning: Open(\"Ram:X/Y/Z/TestCalls-test\", read) "
  106.                    "succeeded.\n");
  107.             Close(file);
  108.         }
  109.         file = Open("ram:X/Y/Z/TestCalls-test", MODE_READWRITE);
  110.         if (file) {
  111.             printf("Warning: Open(\"Ram:X/Y/Z/TestCalls-test\", modify) "
  112.                    "succeeded.\n");
  113.             Close(file);
  114.         }
  115.     }
  116. }
  117.  
  118. /*
  119.  *        FindPort
  120.  */
  121. void Test_FindPort(int trysucc, int tryfail)
  122. {
  123.     if (trysucc) {
  124.         if (!FindPort("AREXX"))
  125.             printf("Warning: FindPort(\"AREXX\") failed.\n");
  126.     }
  127.     if (tryfail) {
  128.         if (FindPort("Illegal Port Name"))
  129.             printf("Warning: FindPort(\"Illegal Port Name\") succeeded.\n");
  130.     }
  131. }
  132.  
  133. /*
  134.  *        FindResident
  135.  */
  136. void Test_FindResident(int trysucc, int tryfail)
  137. {
  138.     if (trysucc) {
  139.         if (!FindResident("con-handler"))
  140.             printf("Warning: FindResident(\"con-handler\") failed.\n");
  141.     }
  142.     if (tryfail) {
  143.         if (FindResident("Illegal Module"))
  144.             printf("Warning: FindResident(\"Illegal Module\") succeeded.\n");
  145.     }
  146. }
  147.  
  148. /*
  149.  *        FindSemaphore
  150.  */
  151. void Test_FindSemaphore(int trysucc, int tryfail)
  152. {
  153.     if (trysucc) {
  154.         if (!FindSemaphore("« IPrefs »"))
  155.             printf("Warning: FindSemaphore(\"« IPrefs »\") failed.\n");
  156.     }
  157.     if (tryfail) {
  158.         if (FindSemaphore("Illegal Semaphore"))
  159.             printf("Warning: "
  160.                    "FindSemaphore(\"Illegal Semaphore\") succeeded.\n");
  161.     }
  162. }
  163.  
  164. /*
  165.  *        FindTask. We check to make sure FindTask(NULL) is ignored also.
  166.  */
  167. void Test_FindTask(int trysucc, int tryfail)
  168. {
  169.     if (trysucc) {
  170.         FindTask(NULL);
  171.         if (!FindTask("DF0"))
  172.             printf("Warning: FindPort(\"DF0\") failed.\n");
  173.     }
  174.     if (tryfail) {
  175.         if (!trysucc)
  176.             FindTask(NULL);
  177.         if (FindTask("Illegal task"))
  178.             printf("Warning: FindTask(\"Illegal task\") succeeded.\n");
  179.     }
  180. }
  181.  
  182. /*
  183.  *        LockPubScreen. Also check to make sure LockPubScreen(NULL) is ignored.
  184.  */
  185. void Test_LockScreen(int trysucc, int tryfail)
  186. {
  187.     void *scr;
  188.  
  189.     if (trysucc) {
  190.         scr = LockPubScreen(NULL);
  191.         if (scr)
  192.             UnlockPubScreen(NULL, scr);
  193.         scr = LockPubScreen("Workbench");
  194.         if (scr)
  195.             UnlockPubScreen(NULL, scr);
  196.         else
  197.             printf("Warning: LockPubScreen(\"Workbench\") failed.\n");
  198.     }
  199.     if (tryfail) {
  200.         if (!trysucc) {
  201.             scr = LockPubScreen(NULL);
  202.             if (scr)
  203.                 UnlockPubScreen(NULL, scr);
  204.         }
  205.         scr = LockPubScreen("Illegal Screen");
  206.         if (scr) {
  207.             printf("Warning: LockPubScreen(\"Illegal Screen\") succeeded.\n");
  208.             UnlockPubScreen(NULL, scr);
  209.         }
  210.     }
  211. }
  212.  
  213. /*
  214.  *        OpenDevice.
  215.  */
  216. void Test_OpenDevice(int trysucc, int tryfail)
  217. {
  218.     struct MsgPort *port;
  219.     struct IORequest *ioreq;
  220.  
  221.     port = CreateMsgPort();
  222.     if (!port) {
  223.         printf("Couldn't create message port for OpenDevice()\n");
  224.         return;
  225.     }
  226.     ioreq = CreateIORequest(port, sizeof(*ioreq));
  227.     if (!ioreq) {
  228.         printf("Couldn't create IO request for OpenDevice()\n");
  229.         DeleteMsgPort(port);
  230.         return;
  231.     }
  232.     if (trysucc) {
  233.         if (OpenDevice("timer.device", UNIT_VBLANK, ioreq, 0) == 0)
  234.             CloseDevice(ioreq);
  235.         else
  236.             printf("Warning: OpenDevice(\"timer.device\") failed.\n");
  237.     }
  238.     if (tryfail) {
  239.         if (OpenDevice("Illegal device", 0, ioreq, 0) == 0)
  240.             printf("Warning: OpenDevice(\"Illegal device\") succeeded.\n");
  241.     }
  242.     DeleteIORequest(ioreq);
  243.     DeleteMsgPort(port);
  244. }
  245.  
  246. /*
  247.  *        OpenFont.
  248.  */
  249. void Test_OpenFont(int trysucc, int tryfail)
  250. {
  251.     struct TextFont *font;
  252.     struct TextAttr textattr;
  253.  
  254.     textattr.ta_Name  = "topaz.font";
  255.     textattr.ta_YSize = 8;
  256.     textattr.ta_Style = FS_NORMAL;
  257.     textattr.ta_Flags = 0;
  258.  
  259.     if (trysucc) {
  260.         font = OpenFont(&textattr);
  261.         if (font)
  262.             CloseFont(font);
  263.         else
  264.             printf("Warning: OpenFont(\"topaz 8\") failed.\n");
  265.     }
  266.     if (tryfail) {
  267.         textattr.ta_Name = "Illegal font";
  268.         font = OpenFont(&textattr);
  269.         if (font) {
  270.             printf("Warning: OpenFont(\"Illegal 8\") succeeded.\n");
  271.             CloseFont(font);
  272.         }
  273.     }
  274. }
  275.  
  276. /*
  277.  *        OpenLibrary.
  278.  */
  279. void Test_OpenLibrary(int trysucc, int tryfail)
  280. {
  281.     void *libbase;
  282.  
  283.     if (trysucc) {
  284.         libbase = OpenLibrary("dos.library", 33);
  285.         if (libbase)
  286.             CloseLibrary(libbase);
  287.         else
  288.             printf("Warning: OpenLibrary(\"dos.library\", 33) failed.\n");
  289.     }
  290.     if (tryfail) {
  291.         libbase = OpenLibrary("Illegal library", 0);
  292.         if (libbase) {
  293.             printf("Warning: OpenLibrary(\"Illegal library\", 0) succeeded.\n");
  294.             CloseLibrary(libbase);
  295.         }
  296.     }
  297. }
  298.  
  299. /*
  300.  *        OpenResource.
  301.  */
  302. void Test_OpenResource(int trysucc, int tryfail)
  303. {
  304.     if (trysucc) {
  305.         if (!OpenResource("ciaa.resource"))
  306.             printf("Warning: OpenResource(\"ciaa.resource\") failed.\n");
  307.     }
  308.     if (tryfail) {
  309.         if (OpenResource("Illegal resource"))
  310.             printf("Warning: OpenResource(\"Illegal resource\") succeeded.\n");
  311.     }
  312. }
  313.  
  314. /*
  315.  *        FindToolTypes
  316.  */
  317. void Test_ReadToolTypes(int trysucc, int tryfail)
  318. {
  319.     static char *tooltypes[] = {
  320.         "ToolType 1",
  321.         "ToolType 2",
  322.         "ToolType 3",
  323.         NULL
  324.     };
  325.     char *tooltype = "VALUE1|VALUE2|VALUE3";
  326.  
  327.     /*
  328.      *        First try FindToolType()
  329.      */
  330.     if (trysucc) {
  331.         if (!FindToolType(tooltypes, "ToolType 1"))
  332.             printf("Warning: FindToolType(\"ToolType 1\") failed.\n");
  333.     }
  334.     if (tryfail) {
  335.         if (FindToolType(tooltypes, "Illegal ToolType"))
  336.             printf("Warning: FindToolType(\"Illegal ToolType\") succeeded.\n");
  337.     }
  338.     /*
  339.      *        Now check MatchToolValue()
  340.      */
  341.     if (trysucc) {
  342.         if (!MatchToolValue(tooltype, "VALUE1"))
  343.             printf("Warning: MatchToolValue(\"VALUE1\") failed.\n");
  344.     }
  345.     if (tryfail) {
  346.         if (MatchToolValue(tooltype, "Illegal Value"))
  347.             printf("Warning: MatchToolValue(\"Illegal Value\") succeeded.\n");
  348.     }
  349. }
  350.  
  351. /*****************************************************************************
  352.  *
  353.  *        Now the table used to call the test functions
  354.  * 
  355.  *****************************************************************************/
  356.  
  357. struct {
  358.     int         type;
  359.     char     *name;
  360.     TestFunc func;
  361. } FuncTable[] = {
  362.  
  363. #define FT(type,name)    type, #name, Test_##name
  364.  
  365.     FT(SYS_FUNC, FindPort),
  366.     FT(SYS_FUNC, FindResident),
  367.     FT(SYS_FUNC, FindSemaphore),
  368.     FT(SYS_FUNC, FindTask),
  369.     FT(SYS_FUNC, LockScreen),
  370.     FT(SYS_FUNC, OpenDevice),
  371.     FT(SYS_FUNC, OpenFont),
  372.     FT(SYS_FUNC, OpenLibrary),
  373.     FT(SYS_FUNC, OpenResource),
  374.     FT(SYS_FUNC, ReadToolTypes),
  375.  
  376.     FT(DOS_FUNC, CurrentDir),
  377.     FT(DOS_FUNC, DeleteFile),
  378.     FT(DOS_FUNC, Execute),
  379.     FT(DOS_FUNC, GetVar),
  380.     FT(DOS_FUNC, LoadSeg),
  381.     FT(DOS_FUNC, Lock),
  382.     FT(DOS_FUNC, MakeDir),
  383.     FT(DOS_FUNC, MakeLink),
  384.     FT(DOS_FUNC, Open),
  385.     FT(DOS_FUNC, Rename),
  386.     FT(DOS_FUNC, RunCommand),
  387.     FT(DOS_FUNC, SetVar),
  388.     FT(DOS_FUNC, System),
  389.     END_FUNC, NULL, NULL
  390. };
  391.  
  392. /*
  393.  *        Mainline
  394.  */
  395. int main(int argc, char **argv)
  396. {
  397.     int trysucceed = 1;
  398.     int tryfail    = 1;
  399.     int numrepeats = 1;
  400.  
  401.     if (CheckSignal(SIGBREAKF_CTRL_C)) {
  402.         printf("^C\n");
  403.         exit(20);
  404.     }
  405.     while (argc > 1 && argv[1][0] == '-') {
  406.         switch (tolower(argv[1][1])) {
  407.             case 'h':    printf(HelpMsg); exit(0);
  408.             case 'f':    trysucceed = 0;     tryfail    = 1;    break;
  409.             case 's':    tryfail    = 0;     trysucceed    = 1;    break;
  410.             default:
  411.                 printf("Unknown option %s -- type TestCalls ? for help\n",
  412.                         argv[1]);
  413.                 break;
  414.         }
  415.         argv++;
  416.         argc--;
  417.     }
  418.     if (argc > 1 && atoi(argv[1]) > 0) {
  419.         numrepeats = atoi(argv[1]);
  420.         argv++;
  421.         argc--;
  422.     }
  423.     if (argc == 1) {
  424.         printf("No functions specified. Type TestCalls ? for help.\n");
  425.         exit(10);
  426.     }
  427.     if (argv[1][0] == '?') {
  428.         printf(HelpMsg);
  429.         exit(0);
  430.     }
  431.  
  432.     /*
  433.      *        Now parse command line options to decide what functions to test
  434.      */
  435.     while (numrepeats-- > 0) {
  436.         int argi;
  437.  
  438.         for (argi = 1; argi < argc; argi++) {
  439.             char *opt = argv[argi];
  440.             int mask = 0;
  441.  
  442.             if (stricmp(opt, "system") == 0)
  443.                 mask = SYS_FUNC;
  444.             else if (stricmp(opt, "dos") == 0)
  445.                 mask = DOS_FUNC;
  446.             else if (stricmp(opt, "all") == 0)
  447.                 mask = ALL_FUNC;
  448.  
  449.             if (mask) {
  450.                 int i;
  451.  
  452.                 for (i = 0; FuncTable[i].type != END_FUNC; i++) {
  453.                     if (FuncTable[i].type & mask)
  454.                         FuncTable[i].func(trysucceed, tryfail);
  455.                     if (SetSignal(0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C) {
  456.                         printf("^C\n");
  457.                         exit(20);
  458.                     }
  459.                 }
  460.             } else {
  461.                 int i;
  462.  
  463.                 for (i = 0; FuncTable[i].type != END_FUNC; i++) {
  464.                     if (stricmp(opt, FuncTable[i].name) == 0) {
  465.                         FuncTable[i].func(trysucceed, tryfail);
  466.                         break;
  467.                     }
  468.                 }
  469.                 if (FuncTable[i].type == END_FUNC) {
  470.                     printf("Warning: Unrecognised function name '%s' "
  471.                            "ignored.\n", opt);
  472.                 }
  473.             }
  474.         }
  475.     }
  476.     return (0);
  477. }
  478.